package com.example.helloandroidworld;

//mine
//import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.widget.SearchView.OnQueryTextListener;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.ListView;


import android.app.ListActivity;
import android.database.Cursor;

public class MessagingContacts extends SherlockListActivity implements /*OnQueryTextListener,*/ LoaderManager.LoaderCallbacks<Cursor>
{
	SimpleCursorAdapter contactsAdapter; // updates data in the listView
	// filter user provided
	
	//private String newQuery;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.messagingcontacts);  
		// to use setContentView( ) in a listactivity
		// requires the line ' android:id="@android:id/list" ' in the xml document
		// or in code
		setUpAdapter();
		
	//	LoaderManager lm = getSupportLoaderManager()
	//	lm.initLoader(arg0, arg1, contactCallbacks);
	}
	

    
    // delete here
    static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
        Contacts._ID,
        Contacts.DISPLAY_NAME,
        Contacts.CONTACT_STATUS,
        Contacts.CONTACT_PRESENCE,
        Contacts.PHOTO_ID,
        Contacts.LOOKUP_KEY,
    };
    // delete up until here

//****************************************** SimpleCursorAdapter *******************************************************************
	
	private void setUpAdapter()
	{
		//SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER		
		contactsAdapter = new SimpleCursorAdapter(this, R.layout.mycontactsrow , null, selectFrom(), modifyTo(), 0);
		setListAdapter(contactsAdapter);
	}	
	
	private String[] selectFrom()
	{
		String[] columns = new String[2];
		columns[0] = Contacts.DISPLAY_NAME;
		columns[1] = Contacts.CONTACT_STATUS;
		return columns;
	}

	private int[] modifyTo() 
	{ // specifies which items to update in the listview
		int modifiableItems[] = new int[2];
		modifiableItems[0] = R.id.myContactRowText;
		modifiableItems[1] = R.id.myContactRowFiller;
		return modifiableItems;
	}


//********************** OnQueryTextListener *****************************************************************************
   /*
	public boolean onQueryTextChange(String newText) 
    {
		return false;
    }
    
    @Override 
    public boolean onQueryTextSubmit(String query) 
    {
    	return false;
    }*/
    
//******************** LoaderCallbacks ***********************************************************************************
public Loader<Cursor> onCreateLoader(int id, Bundle args) 
    {
		//return null;
    	//   Uri baseUri;
       //    if (mCurFilter != null) 
       //    {
      //  	   baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
     //      } else {
     //          baseUri = Contacts.CONTENT_URI;
     //      }

           // Now create and return a CursorLoader that will take care of
           // creating a Cursor for the data being displayed.
      //     String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
       //            + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
       //            + Contacts.DISPLAY_NAME + " != '' ))";
       //    return new CursorLoader(this, baseUri, CONTACTS_SUMMARY_PROJECTION, select, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
    	   return new CursorLoader (this, ContactsContract.AUTHORITY_URI , null, null, null, null);  	
    }
    
    public void onLoadFinished(Loader<Cursor> loader, Cursor data)
    {
        // Swap the new cursor in.  (The framework will take care of closing the
        // old cursor once we return.)
    	contactsAdapter.swapCursor(data);
        // The list should now be shown.
    }
    
    public void onLoaderReset(Loader<Cursor> loader) 
    {
        // This is called when the last Cursor provided to onLoadFinished()
        // above is about to be closed.  We need to make sure we are no
        // longer using it.
    	contactsAdapter.swapCursor(null);
    }
}